package com.app.base.utlis.result;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;


/**
 * 结果
 * @author yuhag
 * @date 2022/04/20
 */
@ApiModel("统一返回结果")
@Data
public class Result {

    @ApiModelProperty("是否成功")
    private Boolean success;

    @ApiModelProperty("返回码")
    private Integer code;

    @ApiModelProperty("返回信息")
    private String message;

    @ApiModelProperty("返回数据")
    private Map<String, Object> data = new HashMap<>();

    private Result() {
    }

    public static Result ok() {
        Result resultVo = new Result();
        resultVo.setSuccess(true);
        resultVo.setCode(ResultCode.SUCCESS.getCode());
        resultVo.setMessage(ResultCode.SUCCESS.getMsg());
        return resultVo;
    }

    public static Result ok(Object data) {
        Result resultVo = new Result();
        resultVo.setSuccess(true);
        Map<String, Object> map = new HashMap<>();
        map.put("data",data);
        resultVo.setData(map);
        resultVo.setCode(ResultCode.SUCCESS.getCode());
        resultVo.setMessage(ResultCode.SUCCESS.getMsg());
        return resultVo;
    }

    public static Result error() {
        Result resultVo = new Result();
        resultVo.setSuccess(false);
        resultVo.setCode(ResultCode.ERROR.getCode());
        resultVo.setMessage(ResultCode.ERROR.getMsg());
        return resultVo;
    }

    public static Result error(String msg) {
        Result resultVo = new Result();
        resultVo.setSuccess(false);
        resultVo.setCode(ResultCode.ERROR.getCode());
        resultVo.setMessage(msg);
        return resultVo;
    }

    public Result success(Boolean success) {
        this.setSuccess(success);
        return this;
    }

    public Result message(String message) {
        this.setMessage(message);
        return this;
    }

    public Result code(Integer code) {
        this.setCode(code);
        return this;
    }

    public Result data(String key, Object value) {
        this.data.put(key, value);
        return this;
    }

    public Result data(Object value) {
        this.data.put("data", value);
        return this;
    }


    public Result data(Map<String, Object> map) {
        this.setData(map);
        return this;
    }

    public Result codeAndMessage(IResultCode resultCode) {
        this.setCode(resultCode.getCode());
        this.setMessage(resultCode.getMsg());
        return this;
    }
}
